package ai.moran.demo.thrift;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.*;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TServerSocket;

public class UserInfoServiceDemo {

    public static final int SERVER_PORT = 8081;

    public static final int SERVER_PORT1 = 8091;

    public static final int SERVER_PORT2 = 8092;

    public static final int SERVER_PORT3 = 8093;

 

    // 简单的单线程服务模型，一般用于测试

    public void startSimleServer() {

       try {

           System.out.println("UserInfoServiceDemo TSimpleServer start ....");

 

           TProcessor tprocessor = new UserInfoService.Processor<UserInfoService.Iface>(new UserInfoServiceImpl());

           TServerSocket serverTransport = new TServerSocket(SERVER_PORT);

           TServer.Args tArgs = new TServer.Args(serverTransport);

           tArgs.processor(tprocessor);

 

           tArgs.protocolFactory(new TBinaryProtocol.Factory());

          

           TServer server = new TSimpleServer(tArgs);

           server.serve();

 

       } catch (Exception e) {

           System.out.println("Server start error!!!");

           e.printStackTrace();

       }

    }

   

    //线程池服务模型，使用标准的阻塞式IO，预先创建一组线程处理请求。

    public void startTThreadPoolServer() {

       try {

            System.out.println("UserInfoServiceDemo TThreadPoolServer start ....");

 

            TProcessor tprocessor = new UserInfoService.Processor<UserInfoService.Iface>(new UserInfoServiceImpl());

            TServerSocket serverTransport = new TServerSocket(SERVER_PORT1);

            TThreadPoolServer.Args ttpsArgs = new TThreadPoolServer.Args(serverTransport);

            ttpsArgs.processor(tprocessor);

            ttpsArgs.protocolFactory(new TBinaryProtocol.Factory());

 

            // 线程池服务模型，使用标准的阻塞式IO，预先创建一组线程处理请求。

            TServer server = new TThreadPoolServer(ttpsArgs);

            server.serve();

 

       } catch (Exception e) {

           System.out.println("Server start error!!!");

           e.printStackTrace();

       }

    }

   

    // 线程池服务模型，使用标准的阻塞式IO，使用非阻塞式IO

    public void startTNonblockingServer() {

       try {

            System.out.println("UserInfoServiceDemo TNonblockingServer start ....");

 

            TProcessor tprocessor = new UserInfoService.Processor<UserInfoService.Iface>(new UserInfoServiceImpl());

            TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(SERVER_PORT2);

            TNonblockingServer.Args tnbArgs = new TNonblockingServer.Args(tnbSocketTransport);

            tnbArgs.processor(tprocessor);

            

            // 使用非阻塞式IO，服务端和客户端需要指定TFramedTransport数据传输的方式

            tnbArgs.transportFactory(new TFramedTransport.Factory());

            tnbArgs.protocolFactory(new TBinaryProtocol.Factory());

 

            TServer server = new TNonblockingServer(tnbArgs);

            server.serve();

 

       } catch (Exception e) {

           System.out.println("Server start error!!!");

           e.printStackTrace();

       }

    }

   

    //半同步半异步的服务端模型，需要指定为： TFramedTransport 数据传输的方式。

    public void startTHsHaServer() {

       try {

           System.out.println("HelloWorld THsHaServer start ....");

 

           TProcessor tprocessor = new UserInfoService.Processor<UserInfoService.Iface>(new UserInfoServiceImpl());

 

           TNonblockingServerSocket tnbSocketTransport = new TNonblockingServerSocket(SERVER_PORT3);

           THsHaServer.Args thhsArgs = new THsHaServer.Args(tnbSocketTransport);

           thhsArgs.processor(tprocessor);

           thhsArgs.transportFactory(new TFramedTransport.Factory());

           thhsArgs.protocolFactory(new TBinaryProtocol.Factory());

 

           TServer server = new THsHaServer(thhsArgs);

           server.serve();

 

       } catch (Exception e) {

           System.out.println("Server start error!!!");

           e.printStackTrace();

       }

    }

 

    public static void main(String[] args) {

       UserInfoServiceDemo server = new UserInfoServiceDemo();

       server.startSimleServer();

       //server.startTThreadPoolServer();

       //server.startTNonblockingServer();

       //server.startTHsHaServer();

    }

}